package com.cyt.reward.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.reward.dao.RewardMoneyDao;
import com.cyt.reward.model.po.RewardMoney;
import com.cyt.reward.model.vo.RewardMoneyVo;
import com.cyt.reward.service.RewardMoneyService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 打赏功能-打赏金额管理Service层实现类
 *
 * @author rong yuan
 * @date 2019-7-24 11:19:39
 */
@Service("rewardMoneyService")
public class RewardMoneyServiceImpl extends BaseServiceImpl<RewardMoney> implements RewardMoneyService {

    @Resource
    private RewardMoneyDao rewardMoneyDao;

    @Autowired
    public void setBaseDao(RewardMoneyDao rewardMoneyDao) {
        this.baseDao = rewardMoneyDao;
    }

    /**
     * 分页查询
     *
     * @param rewardMoneyVo 接受参数对象
     * @param pageNum       分页数
     * @param pageSize      每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 11:19:39
     */
    @Override
    public ReturnResult page(RewardMoneyVo rewardMoneyVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RewardMoneyVo> list = rewardMoneyDao.selectByCondition(rewardMoneyVo);
        PageInfo<RewardMoneyVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param rewardMoneyVo 参数接受对象
     * @param sysUser       当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 11:19:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(RewardMoneyVo rewardMoneyVo, SysUser sysUser) {
        if (checkMoney(rewardMoneyVo.getId(), rewardMoneyVo.getApplyId(), rewardMoneyVo.getMoney())) {
            return new ReturnResult(ErrorCode.CODE_1, "该打赏金额已设置");
        }
        if (StrUtil.isEmpty(rewardMoneyVo.getId())) {
            RewardMoney rewardMoney = new RewardMoney(sysUser);
            rewardMoney.setApplyId(rewardMoneyVo.getApplyId());
            rewardMoney.setMoney(Double.parseDouble(rewardMoneyVo.getMoney()));
            rewardMoney.setValidState(rewardMoneyVo.getValidState());
            int num = rewardMoneyDao.insert(rewardMoney);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            RewardMoney rewardMoney = rewardMoneyDao.selectByPrimaryKey(rewardMoneyVo.getId());
            rewardMoney.setApplyId(rewardMoneyVo.getApplyId());
            rewardMoney.setMoney(Double.parseDouble(rewardMoneyVo.getMoney()));
            rewardMoney.setValidState(rewardMoneyVo.getValidState());
            rewardMoney.setUpdateTime(new Date());
            rewardMoney.setUpdateUser(sysUser.getId());
            int num = rewardMoneyDao.updateByPrimaryKey(rewardMoney);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    private boolean checkMoney(String id, String applyId, String money) {
        RewardMoneyVo rewardMoneyVo = new RewardMoneyVo();
        rewardMoneyVo.setApplyId(applyId);
        rewardMoneyVo.setMoney(money);
        rewardMoneyVo.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
        List<RewardMoneyVo> rewardMoneyVoList = rewardMoneyDao.selectByCondition(rewardMoneyVo);
        if (StringUtils.isBlank(id)) {
            return rewardMoneyVoList != null && rewardMoneyVoList.size() > 0;
        } else {
            return rewardMoneyVoList != null && rewardMoneyVoList.size() > 0 && !rewardMoneyVoList.get(0).getId().equals(id);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param rewardMoneyVo 参数接受对象
     * @param sysUser       当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 11:19:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(RewardMoneyVo rewardMoneyVo, SysUser sysUser) {
        //要修改的字段
        RewardMoney rewardMoney = new RewardMoney();
        rewardMoney.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        rewardMoney.setUpdateUser(sysUser.getId());
        rewardMoney.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(RewardMoney.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(rewardMoneyVo.getId())) {
            criteria.andIn("id", Arrays.asList(rewardMoneyVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        rewardMoneyDao.updateByExampleSelective(rewardMoney, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * list查询
     *
     * @param rewardMoneyVo 参数接受对象
     * @return List<RewardMoneyVo>
     * @author rong yuan
     * @date 2019/7/25 09:38
     */
    @Override
    public List<RewardMoneyVo> list(RewardMoneyVo rewardMoneyVo) {
        rewardMoneyVo.setValidState(DictCode.BASE_STATUS_NORMAL);
        return rewardMoneyDao.selectByCondition(rewardMoneyVo);
    }
}